package cyx.example.ddd.infrastructure.order.repository;

import cyx.example.ddd.domain.order.model.aggregate.Order;
import cyx.example.ddd.domain.order.repository.OrderRepository;
import cyx.example.ddd.infrastructure.order.converter.OrderConverter;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 订单仓储实现类
 */
@Repository
@RequiredArgsConstructor
public class OrderRepositoryImpl implements OrderRepository {
    private final OrderJpaRepository orderJpaRepository;
    private final OrderConverter orderConverter;

    @Override
    public void save(Order order) {
        orderJpaRepository.save(orderConverter.toData(order));
    }

    @Override
    public Optional<Order> findById(String id) {
        return orderJpaRepository.findById(id)
                .map(orderConverter::toDomain);
    }

    @Override
    public List<Order> findByUserId(String userId) {
        return orderJpaRepository.findByUserId(userId).stream()
                .map(orderConverter::toDomain)
                .collect(Collectors.toList());
    }

    @Override
    public void delete(Order order) {
        orderJpaRepository.deleteById(order.getId());
    }
}